package com.poker.duiued;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.poker.iuious.yhyhjs.jklimj.User;
import com.poker.jkiuus.R;
import com.poker.jkiuus.uyrfgd.AppManager;
import com.poker.jkiuus.uyrfgd.DatabaseHelper;
import com.poker.jkiuus.uyrfgd.MD5Utils;

public class FindPwsActivity extends AppCompatActivity {
    private EditText etUserName, etPhone, etEmail;
    private Button btnValidate;
    private DatabaseHelper databaseHelper;
    private String userName;
    private String phone;
    private String email;
    private Toast currentToast;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_password_layout);
        AppManager.addActivity(this);
        databaseHelper = new DatabaseHelper(this);

        etUserName = findViewById(R.id.et_user_name);
        etPhone = findViewById(R.id.et_phone);
        etEmail = findViewById(R.id.et_email);
        btnValidate = findViewById(R.id.btn_validate);

        btnValidate.setOnClickListener(v -> validateUser());
    }

    private void validateUser() {
        userName = etUserName.getText().toString().trim();
        phone = etPhone.getText().toString().trim();
        email = etEmail.getText().toString().trim();

        if (databaseHelper.isUserValid(userName, phone, email)) {

            showResetPasswordDialog();
        } else {

            if (currentToast!=null){
                currentToast.cancel();
            }

            currentToast = Toast.makeText(FindPwsActivity.this, "The data is incorrect, please double-check!", Toast.LENGTH_SHORT);
            currentToast.show();

            etUserName.setError("The data is incorrect, please double-check!");
            etPhone.setError("The data is incorrect, please double-check!");
            etEmail.setError("The data is incorrect, please double-check!");
        }
    }

    private void showResetPasswordDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_reset_password, null);
        final EditText etNewPassword = dialogView.findViewById(R.id.et_new_password);
        final EditText etConPassword = dialogView.findViewById(R.id.et_confirm_new_password);

        builder.setView(dialogView)
                .setTitle("Reset your password")
                .setCancelable(false)
                .setPositiveButton("Confirm", null)
                .setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());

        AlertDialog dialog = builder.create();
        dialog.setOnShowListener(dialogInterface -> {
            Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(view -> {
                String newPassword = etNewPassword.getText().toString().trim();
                String conPassword = etConPassword.getText().toString().trim();

                if (newPassword.isEmpty()){
                    if (currentToast!=null){
                        currentToast.cancel();
                    }
                    currentToast = Toast.makeText(FindPwsActivity.this, "Please don't leave it blank!", Toast.LENGTH_SHORT);
                    currentToast.show();
                    etNewPassword.setError("");
                }else if (conPassword.isEmpty()){
                    if (currentToast!=null){
                        currentToast.cancel();
                    }
                    currentToast = Toast.makeText(FindPwsActivity.this, "Please don't leave it blank!", Toast.LENGTH_SHORT);
                    currentToast.show();
                    etConPassword.setError("");
                }else if (!newPassword.equals(conPassword)){
                    if (currentToast!=null){
                        currentToast.cancel();
                    }
                    currentToast = Toast.makeText(FindPwsActivity.this, "The password is inconsistent twice!", Toast.LENGTH_SHORT);
                    currentToast.show();
                    etConPassword.setError("The password is inconsistent twice!");
                }else {

                    resetUserPassword(newPassword);
                    dialog.dismiss();
                }
            });
        });
        dialog.show();
    }


    private void resetUserPassword(String newPassword) {
        newPassword = MD5Utils.string2MD5(newPassword);
        databaseHelper.updateDataPws(new User(userName,newPassword));
    }
}

